package top.itmeans.yes.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import top.itmeans.yes.entity.Member;
import top.itmeans.yes.entity.MemberReceiveAddress;
import top.itmeans.yes.mapper.MemberReceiveAddressMapper;
import top.itmeans.yes.portal.service.MemberReceiveAddressService;
import top.itmeans.yes.portal.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 会员收货地址表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddress> implements MemberReceiveAddressService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberReceiveAddressMapper addressMapper;
    @Override
    public int add(MemberReceiveAddress address) {
        Member currentMember = memberService.getCurrentMember();
        address.setMemberId(currentMember.getId());
        return addressMapper.insert(address);
    }

    @Override
    public int delete(Long id) {
        Member currentMember = memberService.getCurrentMember();
        LambdaQueryWrapper<MemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberReceiveAddress::getMemberId,currentMember.getId()).eq(MemberReceiveAddress::getId,id);
        return addressMapper.delete(queryWrapper);
    }

    @Override
    public int update(Long id, MemberReceiveAddress address) {
        address.setId(null);
        Member currentMember = memberService.getCurrentMember();
        LambdaQueryWrapper<MemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberReceiveAddress::getMemberId,currentMember.getId()).eq(MemberReceiveAddress::getId,id);
        if(address.getDefaultStatus()==1){
            //先将原来的默认地址去除
            MemberReceiveAddress record = new MemberReceiveAddress();
            record.setDefaultStatus(0);
            LambdaUpdateWrapper<MemberReceiveAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(MemberReceiveAddress::getMemberId,currentMember.getId())
                    .eq(MemberReceiveAddress::getDefaultStatus,1)
                    .set(MemberReceiveAddress::getDefaultStatus,0);
            addressMapper.update(record,updateWrapper);
        }
        return addressMapper.update(address,queryWrapper);
    }

    @Override
    public List<MemberReceiveAddress> list() {
        Member currentMember = memberService.getCurrentMember();
        LambdaQueryWrapper<MemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberReceiveAddress::getMemberId,currentMember.getId());
        return addressMapper.selectList(queryWrapper);
    }

    @Override
    public MemberReceiveAddress getItem(Long id) {
        Member currentMember = memberService.getCurrentMember();
        LambdaQueryWrapper<MemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberReceiveAddress::getMemberId,currentMember.getId());
        queryWrapper.eq(MemberReceiveAddress::getId,id);
        List<MemberReceiveAddress> addressList = addressMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(addressList)){
            return addressList.get(0);
        }
        return null;
    }
}
